package com.wan37.logic.tradingshop.handler;

import com.wan37.logic.component.Worker;
import com.wan37.logic.db.AsynDBManager;
import com.wan37.logic.domain.entity.UserDO;
import com.wan37.logic.domain.vo.BackpackVO;
import com.wan37.logic.domain.vo.UserVO;
import com.wan37.logic.domain.vo.WeaponryVO;
import com.wan37.logic.email.EmailManager;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.WorkerHandler;
import com.wan37.logic.service.UserService;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.trading.cache.TradingCache;
import com.wan37.logic.tradingshop.TradingShopManager;
import com.wan37.logic.tradingshop.cache.TradingShopCache;
import com.wan37.logic.tradingshop.domain.vo.TradingShopRecordVO;
import com.wan37.logic.tradingshop.domain.vo.TradingShopVO;
import com.wan37.logic.tradingshop.service.TradingShopService;
import com.wan37.protobuf.constant.BackpackLabel;
import com.wan37.protobuf.constant.ShopLabel;

import java.util.*;

/**
 * @author : luoyong
 * @date : 2020-08-05 23:03
 **/
public class AuctionResultHandler implements WorkerHandler {
    private TradingShopCache tradingShopCache;
    private AsynDBManager asynDBManager;
    private String tradingShopId;
    private EmailManager emailManager;
    private TradingShopManager tradingShopManager;
    private TradingShopService tradingShopService;
    public AuctionResultHandler(String tradingShopId){
        this.tradingShopId = tradingShopId;
    }
    @Override
    public void execute(Worker worker) throws Exception {
        this.tradingShopCache = SpringContextHolder.getBean("tradingShopCache");
        this.asynDBManager = SpringContextHolder.getBean("asynDBManager");
        this.emailManager = SpringContextHolder.getBean("emailManager");
        this.tradingShopManager = SpringContextHolder.getBean("tradingShopManager");
        this.tradingShopService = SpringContextHolder.getBean("tradingShopService");

        TradingShopCache.TradingShopIn tradingShopIn = tradingShopCache.getTradingShopInMap().get(tradingShopId);

        //到时间了没人拍
        if (tradingShopIn.getTradingShopRecordVOList().size() == 0){
            if (tradingShopIn.getTradingShopVO().getType() == BackpackLabel.WEAPONRY){
                tradingShopManager.addBuyerWeaponry(tradingShopIn.getTradingShopVO().getUserId(),
                        "拍卖物品的结算，你的物品流拍了。请注意查收", tradingShopIn.getTradingShopVO());
            }else if (tradingShopIn.getTradingShopVO().getType() == BackpackLabel.ITEM){

            }
        }else {
            //记录已经处理过的出价人数据
            Set<String> buyerIdDone = new HashSet<>();
            //处理最高价格的人，进行卖方金币
            TradingShopRecordVO tradingShopRecordVO1 = tradingShopIn.getTradingShopRecordVOList().get(0);
            List<String> userIdList = new ArrayList<>();
            userIdList.add(tradingShopRecordVO1.getUserId());
            emailManager.sendEmail("竞拍结果获得元宝", "拍卖行",
                    userIdList, ShopLabel.MONEY_TYPE_INGOT, tradingShopRecordVO1.getIngot());
            //将拍品发给出价最高者
            if (tradingShopIn.getTradingShopVO().getType() == BackpackLabel.WEAPONRY){
                tradingShopManager.addBuyerWeaponry(tradingShopRecordVO1.getBuyId(),
                        "拍卖物品的结算，您竞拍成功。请注意查收", tradingShopIn.getTradingShopVO());
            }else if (tradingShopIn.getTradingShopVO().getType() == BackpackLabel.ITEM){

            }
            buyerIdDone.add(tradingShopRecordVO1.getBuyId());
            //进行竞拍失败的用户金币返回
            for (TradingShopRecordVO tradingShopRecordVO : tradingShopIn.getTradingShopRecordVOList()){
                if (buyerIdDone.contains(tradingShopRecordVO.getBuyId())){
                    continue;
                }
                List<String> userIdList1 = new ArrayList<>();
                userIdList1.add(tradingShopRecordVO.getBuyId());
                emailManager.sendEmail("竞拍物品 " + tradingShopRecordVO.getBackpackId() +" 失败，元宝返回！", "拍卖行",
                        userIdList1, ShopLabel.MONEY_TYPE_INGOT, tradingShopRecordVO.getIngot());
            }
        }
        //删库，删缓存
        asynDBManager.distributeWork(tradingShopId,
                () -> tradingShopService.deleteByTradingShopId(tradingShopId));
        tradingShopCache.getTradingShopInMap().remove(tradingShopId);
    }
}
